/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sunrise.boundary;

import com.sunrise.entity.Calendar;
import com.sunrise.entity.User;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author frank
 */
@Stateless
public class CalendarManager {

    @PersistenceContext
    private EntityManager entityManager;
    
    public void save(Calendar calendar) {
        
        entityManager.persist(calendar);
        
    }
    
    public List<Calendar> findByUser(User user) {
        List<Calendar> result = new ArrayList<Calendar>();
        
        try {
            result = entityManager.createNamedQuery(Calendar.findByUser, Calendar.class)
                        .setParameter("owner", user)
                        .getResultList();
        } catch (Exception ex) {

        }
        
        return result;
    }
    
    public void displayError(FacesContext context, String msg) {     
        //FacesContext context = FacesContext.getCurrentInstance();
        if(context != null) context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", msg));
    }

    public void displayInfo(FacesContext context, String msg) {
        if(context != null) context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Warning", msg));
    }
}
